﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace CreateDirectory
{
	public static class DirectoryHelper
	{
		public static string CombinPath(Directory parent, XElement son)
		{
			return Path.Combine(parent.Path, son.Attribute("name").Value);
		}

		public static List<Directory> GetDirectoryTreeByConfig(string filename, string taskname)
		{
			List<Directory> list = new List<Directory>();
			XElement config = XElement.Load(filename);
			if (config.Element("directory") == null)
			{
				return null;
			}
			foreach (XElement dir in config.Elements("directory"))
			{
				string name = dir.Attribute("name").Value.Replace("#TASKNAME#", taskname);
				name = name.Replace("#TIMESTAMP#", DateTime.Now.ToString("yyyyMMdd"));
				Directory directory = new Directory(name, name);
				list.Add(directory);
				GetSubDirectory(dir, directory);
			}
			return list;
		}

		private static void GetSubDirectory(XElement parent, Directory parentDirectory)
		{
			foreach (XElement dir in parent.Elements("directory"))
			{
				Directory directory = new Directory(dir.Attribute("name").Value, DirectoryHelper.CombinPath(parentDirectory, dir));
				parentDirectory.Child.Add(directory);
				GetSubDirectory(dir, directory);
			}
		}
	}
}
